﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace SwirlTree
{
    [Serializable()]
    public class SwirlParametersBase : TreeNode, ISerializable
    {
        public double curX, curY;
        public double width;

        public double radius = 50;
        public double targetRadius = 20;
        public double targetWidth = 1.0;
        public double angle = -10;

        public int steps = 200;

        public bool inheritColor;
        public CMYK startColor;
        public CMYK endColor;

        protected bool _isRoot;

        private List<SwirlParameterChild> swirlsDeser;

        public bool isRoot
        {
            get { return this._isRoot; }
        }

        public SwirlParametersBase(string text)
        {
            this.Text = text;
            _isRoot = true;
            inheritColor = false;
            startColor = ColorStuff.RGBtoCMYK(Color.Black);
            endColor = ColorStuff.RGBtoCMYK(Color.Black);
        }
        public SwirlParametersBase(SerializationInfo info, StreamingContext ctx)
        {
            this.curX = (double)info.GetValue("curX", typeof(double));
            this.curY = (double)info.GetValue("curY", typeof(double));
            this.width = (double)info.GetValue("width", typeof(double));
            this.radius = (double)info.GetValue("radius", typeof(double));
            this.targetRadius = (double)info.GetValue("targetRadius", typeof(double));
            this.targetWidth = (double)info.GetValue("targetWidth", typeof(double));
            this.angle = (double)info.GetValue("angle", typeof(double));
            this.steps = (int)info.GetValue("steps", typeof(int));
            this._isRoot =(bool)info.GetValue("isRoot", typeof(bool));
            this.Text = (string)info.GetValue("title", typeof(string));
            this.inheritColor = (bool)info.GetValue("inheritColor", typeof(bool));
            this.startColor = (CMYK)info.GetValue("startColor", typeof(CMYK));
            this.endColor = (CMYK)info.GetValue("endColor", typeof(CMYK));

            swirlsDeser = (List<SwirlParameterChild>)info.GetValue("nodes", typeof(List<SwirlParameterChild>));
        }

        [OnDeserialized]
        public void OnDeserialized(StreamingContext context)
        {
            foreach(SwirlParameterChild s in swirlsDeser)
            {
                this.Nodes.Add(s);
            }
            swirlsDeser = null;
        }
        public void GetObjectData(SerializationInfo info, StreamingContext ctx)
        {
            info.AddValue("curX", curX);
            info.AddValue("curY", curY);
            info.AddValue("width", width);
            info.AddValue("radius", radius);
            info.AddValue("targetRadius", targetRadius);
            info.AddValue("targetWidth", targetWidth);
            info.AddValue("angle", angle);
            info.AddValue("steps", steps);
            info.AddValue("isRoot", _isRoot);
            info.AddValue("title", this.Text);
            info.AddValue("inheritColor", this.inheritColor);
            info.AddValue("startColor", this.startColor);
            info.AddValue("endColor", this.endColor);

            List<SwirlParameterChild> childs = new List<SwirlParameterChild>();
            for (int i = 0; i < this.Nodes.Count; i++)
            {
                SwirlParameterChild swc = (SwirlParameterChild)this.Nodes[i];
                childs.Add((SwirlParameterChild)this.Nodes[i]);
            }
            info.AddValue("nodes", childs);
        }

        public void Add(SwirlParameterChild child)
        {
            this.Nodes.Add(child);
        }
    }

    [Serializable()]
    public class SwirlParameterChild : SwirlParametersBase, ISerializable
    {
        public double startPercentage = 0.5;
        public double endPercentage = 0.5;
        public int numSwirls = 1;

        public SwirlParameterChild(string text) : base(text)
        {
            _isRoot = false;
            inheritColor = true;
        }
        public SwirlParameterChild(SerializationInfo info, StreamingContext ctx) : base(info, ctx)
        {
            this.startPercentage = (double)info.GetValue("startPercentage", typeof(double));
            this.endPercentage = (double)info.GetValue("endPercentage", typeof(double));
            this.numSwirls = (int)info.GetValue("numSwirls", typeof(int));
        }
        public new void GetObjectData(SerializationInfo info, StreamingContext ctx) 
        {
            base.GetObjectData(info, ctx);
            info.AddValue("startPercentage", startPercentage);
            info.AddValue("endPercentage", endPercentage);
            info.AddValue("numSwirls", numSwirls);
        }
    }
}
